package org.social.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.constant.ActionEnum;
import org.constant.ActivityConstant;
import org.error.GameErrorCode;
import org.social.db.bean.Activity;
import org.social.db.bean.ActivityEntry;
import org.social.db.cache.ActivityCache;
import org.social.db.cache.ActivityEntryCache;
import org.social.manager.ActivityManager;

import com.dal.gm_api.api.IActivity;
import com.dal.gm_api.constant.ActivityAttrKey;
import com.google.common.collect.Maps;
import com.sojoys.artifact.constant.DataModelKeyEnum;
import com.sojoys.artifact.manager.JsonManager;
import com.sojoys.artifact.tools.ToolError;
import com.xiaoleilu.hutool.collection.CollUtil;

/**
 * @author : DengYing
 * @CreateDate : 2018年11月4日 下午5:00:13
 * @Description ：Please describe this document
 */
public class ActivityServer implements IActivity {
	@Override
	public void createActivity(int type,Date startDate, Date endDate,int resetType, int dataType,int rank,Map remark) {
		int id = 0;
		int status = ActivityConstant.AWAIT;
		boolean active = false;
		Date resetDate = new Date();
		int ver = 0;
		Activity activity = new Activity(id, status, active, startDate, endDate,resetDate, type,resetType, dataType, remark, rank,ver);
		activity.insert();
	}
	
	@Override
	public void createEntry(int activityId,int finishCondId,Map finishParams,Map reward,int rank,Map remark) {
		int progress = 0;
		boolean active = false;
		Map extend = Maps.newHashMap();
		ActivityEntry entry = new ActivityEntry(0, activityId, active, finishCondId, finishParams, progress, reward, extend, remark, rank);
		entry.insert();
	}
	
	@Override
	public void activeActivity(List<Integer> ids, boolean active) {
		for (Integer id : ids) {
			Activity activity = ActivityCache.me().getById(id);
			
			ToolError.isAndTrue(GameErrorCode.NOT_FOND_ACTIVITY, activity == null);
			
			if(activity.isActive()!=active){
				activity.setActive(active);
				activity.update();
			}
		}
	}
	
	
	@Override
	public void resetActivity(List<Integer> ids) {
		for (Integer id : ids) {
			Activity activity = ActivityCache.me().getById(id);
			activity.setStatus(ActivityConstant.AWAIT);
			activity.update();
		}
	}
	
	@Override
	public void activeEntry(List<Integer> ids, boolean active) {
		for (Integer id : ids) {
			
			ActivityEntry entry = ActivityEntryCache.me().getById(id);
			
			ToolError.isAndTrue(GameErrorCode.NOT_FOND_ACTIVITY_ENTRY, entry == null);
			
			Activity activity = ActivityCache.me().getById(entry.getActivityId());
			ToolError.isAndTrue(GameErrorCode.NOT_FOND_ACTIVITY, activity == null);
			
			if(entry.isActive()!=active){
				entry.setActive(active);
				entry.update();
			}
			
			List<Activity> list = new ArrayList<>();
			list.add(activity);
			ActivityManager.me().notifyActivity(list,ActionEnum.UPDATE);
		}
	}
	


	@Override
	public List<String> getAllActivity() {
		List<Activity> activitys = ActivityCache.me().loadAll();
		List<String> result = new ArrayList<>(activitys.size());
		
		for (Activity activity : activitys) {
			Map map = activity.toMap(DataModelKeyEnum.SOURCE);
			
			
			List<ActivityEntry> entrys = activity.getActivityEntrysFkActivityId();
			List<Map> entryInfos = new ArrayList<>(entrys.size());
			for (ActivityEntry activityEntry : entrys) {
				entryInfos.add(activityEntry.toMap(DataModelKeyEnum.SOURCE));
			}
			map.put("entrys", entryInfos);
			result.add(JsonManager.me().getJson().toJson(map));	
		}
		return result;
	}

	@Override
	public void saveActivity(int activityId,Map<Integer, Object> attr) {
		Activity activity = ActivityCache.me().getById(activityId);
		ToolError.isAndTrue(GameErrorCode.NOT_FOND_ACTIVITY, activity == null);
		for (Entry<Integer, Object> e : attr.entrySet()) {
			switch (e.getKey()) {
			case ActivityAttrKey.ACTIVITY_START_DATE:
				activity.setStartDate((Date) e.getValue());
				break;
			case ActivityAttrKey.ACTIVITY_END_DATE:
				activity.setEndDate((Date) e.getValue());			
				break;
			case ActivityAttrKey.ACTIVITY_RESET_TYPE:
				activity.setResetType((int) e.getValue());
				break;
			case ActivityAttrKey.ACTIVITY_DATA_TYPE:
				activity.setDataType((int) e.getValue());
				break;
			case ActivityAttrKey.ACTIVITY_ACTIVITY_RANK:
				activity.setRank((int) e.getValue());
				break;
			case ActivityAttrKey.ACTIVITY_REMARK:
				activity.setRemark((Map) e.getValue());
				break;
			}
		}
		activity.update();
		
		List<Activity> list = new ArrayList<>();
		list.add(activity);
		ActivityManager.me().notifyActivity(list,ActionEnum.UPDATE);
	}

	@Override
	public void saveEntry(int entryId,Map<Integer, Object> attr) {
		ActivityEntry entry = ActivityEntryCache.me().getById(entryId);
		ToolError.isAndTrue(GameErrorCode.NOT_FOND_ACTIVITY_ENTRY, entry == null);
		
		Activity activity = ActivityCache.me().getById(entry.getActivityId());
		ToolError.isAndTrue(GameErrorCode.NOT_FOND_ACTIVITY, activity == null);
		
		for (Entry<Integer, Object> e : attr.entrySet()) {
			switch (e.getKey()) {
			case ActivityAttrKey.ENTRY_FINISH_COND_ID:
				entry.setFinishCondId((int) e.getValue());
				break;
			case ActivityAttrKey.ENTRY_FINISH_PARAMS:
				entry.setFinishParams((Map) e.getValue());			
				break;
			case ActivityAttrKey.ENTRY_FINISH_REWARD:
				entry.setReward((Map) e.getValue());
				break;
			case ActivityAttrKey.ENTRY_FINISH_RANK:
				entry.setRank((int) e.getValue());
				break;
			case ActivityAttrKey.ENTRY_FINISH_REMARK:
				entry.setRemark((Map) e.getValue());
				break;
			}
		}
		entry.update();
		
		List<Activity> list = new ArrayList<>();
		list.add(activity);
		ActivityManager.me().notifyActivity(list,ActionEnum.UPDATE);
	}

	@Override
	public void deleteActivity(List<Integer> ids) {
		
		List<Activity> list = new ArrayList<>();
		
		for (Integer id : ids) {
			Activity activity = ActivityCache.me().getById(id);
			activity.delete();
			list.add(activity);
		}
		
		ActivityManager.me().notifyActivity(list,ActionEnum.REMOVE);
	}

	@Override
	public void deleteEntry(List<Integer> ids) {
		List<Activity> list = new ArrayList<>();
		for (Integer id : ids) {
			ActivityEntry entry = ActivityEntryCache.me().getById(id);
			entry.delete();
			if (entry.isActive()) {
				Activity activity = ActivityCache.me().getById(entry.getActivityId());
				list.add(activity);
			}
		}
		if (list.isEmpty()) {
			ActivityManager.me().notifyActivity(list,ActionEnum.UPDATE);			
		}
	}

	
}
